package com.lxhll.personnel.common;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;

@Component
public class OssUtils {
    // Endpoint
    @Value("${aliyun.oss.endpoint}")
    private  String endpoint;
    // 阿里云主账号AccessKey
    @Value("${aliyun.oss.accessKeyId}")
    private  String accessKeyId;

    @Value("${aliyun.oss.accessKeySecret}")
    private  String accessKeySecret;

    @Value("${aliyun.oss.bucketName}")
    private  String bucketName;

    public  String uploadFile(String file) throws Exception{
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        String fileName = "xxx.txt";
        
        String content = "Hello OSS";
        // 上传文件到指定的存储空间（bucketName）并将其保存为指定的文件名称（objectName）。
        ossClient.putObject(bucketName, fileName, new ByteArrayInputStream(content.getBytes()));
        // 关闭OSSClient。
        ossClient.shutdown();
        return "success";
    }
}
